package com.ptmall.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.CategoryBean;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.CateListActivity;
import com.ptmall.app.ui.activity.SeachActivity;
import com.ptmall.app.ui.adapter.CateGoryAdapter;

import java.util.List;

public class CategoryFragment extends BaseMvpFragment {

    View view;
    View seach;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    CateGoryAdapter mCateGoryAdapter;

    GridView category_gridview;

    Intent intent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_category,container,false);
        seach = view.findViewById(R.id.seach);
        return view;
    }

    @Override
    public void initViews() {
        category_gridview = (GridView) view.findViewById(R.id.category_gridview);
    }

    @Override
    public void initEvents() {
        seach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), SeachActivity.class));
            }
        });
        mCateGoryAdapter = new CateGoryAdapter(getContext(),null);
        category_gridview.setAdapter(mCateGoryAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        getCate();
    }

    private void getCate(){
        apiDataRepository.getCategory(null, new ApiNetResponse<List<CategoryBean>>() {
            @Override
            public void onSuccess(List<CategoryBean> data) {
                mCateGoryAdapter.pushData(data);
                category_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        intent = new Intent(getContext(), CateListActivity.class);
                        intent.putExtra("id",data.get(i).styleId);
                        startActivity(intent);
                    }
                });
            }
        });
    }
}
